
import { scrollMixin } from '../../utils/mixin';
import {Dialog} from "vant";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      active: 0,
      show: false,
      type: '',
      get_time_sort: "asc",
      active_end_sort: "asc",
      keyword: "",
      timerId: null,
      show_tips: false,
      member_list:[],
      integral:"积分",

      //分享信息
      JYB: "",//兑换卷名称
      validity: "",//有效期



      id: "",//兑换码记录id
      code_id: "",//兑换码id
      uid: '',
      nickname: "",
      name: "",
      code_sn:"",
      show_member_flag:false,

      list: [],
      isLoadMore: false,
      current_page: 1,
      last_page: 1,

      avatar:"",
    };
  },
  activated() {
    this.show_member_flag = false;
    this.isLoadMore = false;
    this.get_time_sort = 'desc';
    this.integral = localStorage.getItem('integral')||localStorage.getItem('integralLang')||'积分';
    let json = this.get_tab_params();
    this.getData(json)
  },
  methods: {
    input_handle() {
      if (this.keyword) {
        if (this.timerId) {
          clearTimeout(this.timerId)
        }
        this.timerId = setTimeout(() => {
          this.getMemberInfo()
        }, 600);
      }
    },
    choose_handle({uid,mobile,nickname,name,avatar}){
      this.avatar = avatar;
      this.uid = uid;
      this.nickname = nickname;
      this.name = name;
      this.keyword=mobile || uid;
      this.show_member_flag = false;
    },
    async getMemberInfo() {
      let { result, msg, data: { data } } = await $http.get("plugin.exchange-code.api.record.memberSearch", { keyword: this.keyword });
      if (result) {
        if (data && data.length !== 0) {
          this.member_list = data;
          this.show_tips = false;
          this.show_member_flag = true;
        }
        if (data.length == 0) {
          this.member_list = [];
          this.nickname = '';
          this.name = '';
          this.uid = '';
          this.show_tips = true;
          this.show_member_flag = false;
        }
      } else {
        this.$toast(msg);
      }
    },
    change() {

      let json = {};
      if (this.active == 2 || this.active == 3 || this.active==4) {
        json.search = {
          type: this.active - 1
        }
      }
      if (this.active == 0 || this.active == 1) {
        if (this.active == 0) {
          if (this.get_time_sort == 'asc') {
            this.get_time_sort = 'desc'
          } else {
            this.get_time_sort = 'asc'
          }
          json.order = { get_time: this.get_time_sort };
        } else {
          if (this.active_end_sort == 'asc') {
            this.active_end_sort = 'desc'
          } else {
            this.active_end_sort = 'asc'
          }
          json.order = { active_end: this.active_end_sort };
        }
      }
      this.getData(json);
    },
    get_tab_params(){
      let json = {};
      if (this.active == 2 || this.active == 3) {
        json.search = {
          type: this.active - 1
        }
      }
      if (this.active == 0 || this.active == 1) {
        if (this.active == 0) {
          json.order = { get_time: this.get_time_sort };
        } else {
          json.order = { active_end: this.active_end_sort };
        }
      }
      return json;
    },
    getMoreData() {
      if (this.current_page >= this.last_page) {
        return;
      }
      this.isLoadMore = false;
      let json = this.get_tab_params();
      this.getData({page: this.current_page + 1,...json})
    },
    async getData(json) {
      let { result, data: { data, current_page, last_page }, msg } = await $http.post("plugin.exchange-code.api.record.list", json,'loading');
      this.isLoadMore = true;
      if (result) {
        this.current_page = current_page;
        this.last_page = last_page;
        if (current_page == 1) {
          this.list = data || [];
        } else {
          this.list = this.list.concat(data);
        }
      } else {
        this.$toast(msg)
      }
    },
    async confirm() {
      if (this.type == 'binding') {
        let { result, msg } = await $http.get('plugin.exchange-code.api.record.bind', { sn: this.code_id })
        this.$toast(msg)
        if (result) {
          this.show = false;
          let json = {};
          if (this.active == 2 || this.active == 3) {
            json.search = {
              type: this.active - 1
            }
          }
          if (this.active == 0 || this.active == 1) {
            if (this.active == 0) {
              json.order = { get_time: 'desc' };
            } else {
              json.order = { active_end: 'desc' };
            }
          }
          this.getData(json)
        }
      } else {
        Dialog.confirm({
          message: '是否确认转赠给'+this.nickname,
        })
          .then(async() => {
            let { result, msg ,data:{title,icon,desc}} = await $http.get('plugin.exchange-code.api.record.tranfer', { uid: this.uid, id: this.id })
            this.$toast(msg);
            if (result) {
              localStorage.setItem("wxShareInfo",JSON.stringify({title,imgUrl:icon,description:desc}))
              setTimeout(() => {
                this.$router.push(this.fun.getUrl('redeem_code', {share: 1},{code_id:this.code_id,sn_code:this.code_sn,show:false}))
              }, 500);
            }
          })
          .catch(() => {
            // on cancel
          });

      }
    },
    link(data) {
      this.$router.push(this.fun.getUrl('redeem_code',{},{code_id:data.code_id,sn_code:data.code_sn}))
    },
    code_handle(type, item) {
      if (item) {
        this.id = item.id;
        this.code_sn = item.code_sn;
        this.code_id=item.code_id
        this.JYB = item.name;
        this.validity = item.active_end;
      }
      this.show_tips = false;
      this.type = type;
      this.show = true;
    },
  },

};